package me.fm.controller.admin;


import me.fm.cloud.model.User;
import me.fm.controller.BaseController;
import me.fm.service.UserService;
import me.fm.util.SessionUtil;
import me.fm.util.WebConst;

import org.unique.common.tools.StringUtils;
import org.unique.ioc.annotation.Autowired;
import org.unique.web.annotation.Action;
import org.unique.web.annotation.Path;
import org.unique.web.render.impl.PatchcaRender;

/**
 * 用户后台首页
 * @author:rex
 * @date:2014年8月19日
 * @version:1.0
 */
@Path("/admin")
public class IndexController extends BaseController {

	@Autowired
	private UserService userService;
	
	/**
	 * 后台首页
	 */
	public void index() {
		this.render("index");
	}

	/**
	 * 管理员退出
	 */
	public void logout() {
		SessionUtil.removeLoginUser();
		this.redirect("/");
	}

	/**
	 * 用户登录
	 */
	public void login() {
		String step = this.getPara("step");
		if (null != step && step.equals("login")) {
			String email = this.getPara("login_name");
			String verify_code = this.getPara("verify_code");
			String pass_word = this.getPara("pass_word");
			String currentcode = SessionUtil.getVerifyCode(this.getRequest());
			if(StringUtils.isNotBlank(verify_code) && verify_code.equalsIgnoreCase(currentcode)){
				User user = userService.login(email, pass_word);
				//登录成功
				if (null != user) {
					SessionUtil.setLoginUser(user);
					this.renderText(WebConst.MSG_SUCCESS);
				} else {
					this.renderText(WebConst.MSG_FAILURE);
				}
			} else{
				this.renderText(WebConst.MSG_VERIFY_ERROR);
			}
			return;
		}
		this.render("login");
	}
	
	@Action("/verify_code")
	public void verify_code() {
		this.render(new PatchcaRender());
	}
}
